package com.ruoyi.project.system.controller;

import java.util.List;
import java.util.UUID;

import com.ruoyi.project.system.domain.Cinema;
import com.ruoyi.project.system.service.ICinemaService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.MovieShow;
import com.ruoyi.project.system.service.IMovieShowService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 电影厅Controller
 * 
 * @author ruoyi
 * @date 2021-05-18
 */
@RestController
@RequestMapping("/system/show")
public class MovieShowController extends BaseController
{
    @Autowired
    private IMovieShowService movieShowService;

    @Autowired
    private ICinemaService cinemaService;

    /**
     * 查询电影厅列表
     */
    @PreAuthorize("@ss.hasPermi('system:show:list')")
    @GetMapping("/list")
    public TableDataInfo list(MovieShow movieShow)
    {
        List<Cinema> cinemaList = cinemaService.selectCinemaList(new Cinema());
        startPage();

        List<MovieShow> list = movieShowService.selectMovieShowList(movieShow);
        for(MovieShow show : list){
            for(Cinema cinema : cinemaList){
                if(show.getCinemaId().equals(cinema.getId())){
                    show.setCinemaId(cinema.getName());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出电影厅列表
     */
    @PreAuthorize("@ss.hasPermi('system:show:export')")
    @Log(title = "电影厅", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MovieShow movieShow)
    {
        List<MovieShow> list = movieShowService.selectMovieShowList(movieShow);
        ExcelUtil<MovieShow> util = new ExcelUtil<MovieShow>(MovieShow.class);
        return util.exportExcel(list, "电影厅数据");
    }

    /**
     * 获取电影厅详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:show:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(movieShowService.selectMovieShowById(id));
    }

    /**
     * 新增电影厅
     */
    @PreAuthorize("@ss.hasPermi('system:show:add')")
    @Log(title = "电影厅", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MovieShow movieShow)
    {
        movieShow.setId(UUID.randomUUID().toString().replace("-", ""));
        return toAjax(movieShowService.insertMovieShow(movieShow));
    }

    /**
     * 修改电影厅
     */
    @PreAuthorize("@ss.hasPermi('system:show:edit')")
    @Log(title = "电影厅", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MovieShow movieShow)
    {
        return toAjax(movieShowService.updateMovieShow(movieShow));
    }

    /**
     * 删除电影厅
     */
    @PreAuthorize("@ss.hasPermi('system:show:remove')")
    @Log(title = "电影厅", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(movieShowService.deleteMovieShowByIds(ids));
    }


    @GetMapping(value = "/searchCinema")
    public AjaxResult searchCinema()
    {
        return AjaxResult.success(cinemaService.selectCinemaList(new Cinema()));
    }

    @GetMapping("/searchShow")
    public AjaxResult searchShow(){
        List<MovieShow> movieShows = movieShowService.selectMovieShowList(new MovieShow());
        return AjaxResult.success(movieShows);
    }
}
